package com.common.emums;

import com.common.ErrorCodeEnum;
import com.common.exception.BusinessException;

public enum UserType {
    //枚举值
    USER((byte) 0, "用户"),
    ADMIN((byte) 1, "管理员"),
    Leader((byte) 2, "生产厂商"),
    ANONYMOUS((byte) 3, "分管理员");

    private final Byte code;
    private final String info;

    UserType(Byte code, String info) {
        this.code = code;
        this.info = info;
    }

    public boolean equals(Byte code){
        if(this.code.equals(code)) {
            return true;
        }
        return false;
    }

    public static Byte getCode(String info){
       for(UserType userType:UserType.values()){
           if(userType.getInfo().equals(info)){
               return userType.getCode();
           }
       }
     throw new BusinessException(ErrorCodeEnum.USER_TYPE_FAILED.setParam("info:" + info));
    }

    public static String getInfo(Byte code){
        for(UserType userType:UserType.values()){
            if(userType.getCode().equals(code)){
                return userType.getInfo();
            }
        }
        throw new BusinessException(ErrorCodeEnum.USER_TYPE_FAILED.setParam("code:" + code));
    }

    public static boolean contains(Byte code){
        if(code >=0 && code<=2){
            return true;
        }
        return false;
    }

    public static boolean containsResident(Byte code){
        if(code >=0 && code<=1){
            return true;
        }
        return false;
    }

    public Byte getCode() {
        return code;
    }

    public String getInfo() {
        return info;
    }
}
